package game.game;

import com.crunch.core.Actor;
import com.crunch.core.Renderable;
import com.crunch.graphics.gpu.GpuState;
import com.crunch.math.Vector2f;
import com.crunch.physics.*;

/**
 * Created by njanetos on 2/25/14.
 */
public class Door extends Actor implements Renderable {

    MovablePhysicsEntity door;
    // whether the door is open or not
    public boolean open = false;
    private int position = 0;
    private int timer;

    public Door() {
        super(0);
    }

    @Override
    public void onCreate() {
        RoomState roomState = (RoomState) getGameState();
        PhysicsSimulation physicsSimulation = roomState.getPhysicsSimulation();

        Vector2f[] vertices = new Vector2f[] {
                new Vector2f(0.5f, 3.0f),
                new Vector2f(-0.5f, 3.0f),
                new Vector2f(-0.5f, 0.0f),
                new Vector2f(0.5f, 0.0f)
        };
        com.crunch.physics.Polygon polygon = new Polygon(vertices);

        // Create the actual door
        MovablePhysicsEntity.Description desc = new MovablePhysicsEntity.Description();
            desc.shape = polygon;
            desc.rotation = 0.0f;
            desc.initialPosition = new Vector2f(10.0f, 4.0f);
            desc.initialVelocity = new Vector2f(0.0f, 0.0f);
            desc.maxSquishAngle = (float) Math.PI * 0.25f;
            desc.eventHandler = new MovablePhysicsEntity.EventHandler() {
                @Override
                public void onSquish(MovablePhysicsEntity movableEntity, DynamicPhysicsEntity dynamicEntity) {
                    System.out.println("Squish " + System.currentTimeMillis());
                }
            };
        door = physicsSimulation.addMovableEntity(desc);

        // Create a proximity zone around the door
        PhysicsProximityZone.Description pzDesc = new PhysicsProximityZone.Description();
            pzDesc.shape = PhysicsProximityZone.Shape.RECTANGLE;
            pzDesc.boundMin = new Vector2f(7.5f, 4.0f);
            pzDesc.boundMax = new Vector2f(12.5f, 10.0f);
            pzDesc.categoryMask = CategoryMask.addCategoryToMask(CategoryMask.MASK_NO_CATEGORIES, 0);
            pzDesc.data = this;
            pzDesc.eventHandler = new PhysicsProximityZone.EventHandler() {
                @Override
                public void onIntersect(PhysicsProximityZone proximityZone, DynamicPhysicsEntity entity) {}

                @Override
                public void onEnter(PhysicsProximityZone proximityZone, DynamicPhysicsEntity entity) {
                    ((Door) proximityZone.getData()).open = true;
                }

                @Override
                public void onExit(PhysicsProximityZone proximityZone, DynamicPhysicsEntity entity) {
                    ((Door) proximityZone.getData()).open = false;
                }
            };
        physicsSimulation.addProximityZone(pzDesc);
    }

    @Override
    public void render(GpuState gpuState) {

    }

    @Override
    public int getDepth() {
        return 0;
    }

    @Override
    public void onUpdate() {
        if (open) {
            timer = 15;
            ++position;
            if (position > 10) {
                position = 10;
                door.setVelocity(new Vector2f(0.0f, 0.0f));
            }
            else {
                door.setVelocity(new Vector2f(0.0f, 15.0f));
            }
        }
        else {
            --timer;
            if (timer <= 0) {
                --position;
                if (position >= 0) {
                    door.setVelocity(new Vector2f(0.0f, -15.0f));
                }
                else {
                    position = 0;
                    door.setVelocity(new Vector2f(0.0f, 0.0f));
                }
            }
        }
    }
}
